package com.oocl.aeat.redis;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.oocl.aeat.dto.MerchantDto;
import com.oocl.aeat.pojo.Merchant;
import com.oocl.aeat.service.MerchantService;
import com.oocl.aeat.service.factory.MerchantServiceFactory;
import com.oocl.aeat.util.Const;

/**
 * 商户缓存
 * @author GANAB
 *
 */
public class MerchantCache {
	private volatile static Map<String, MerchantDto> cache;

	private MerchantCache() {

	}

	public static Map<String, MerchantDto> getCache() {
		return cache;
	}

	public static void add(MerchantDto dto){
		cache.put(dto.getId(), dto);
	}
	
	public static void delete(String id){
		cache.remove(id);
	}
	
	public static void update(MerchantDto dto) {
		cache.remove(dto.getId());
		cache.put(dto.getId(), dto);
	}

	public static void init() {
		try {
			cache = new ConcurrentHashMap<String, MerchantDto>();
			MerchantService service = MerchantServiceFactory.getInstance();
			List<Merchant> list = service.queryAllMerchant(Const.BLOCK_WHITE);
			List<MerchantDto> dtoList = service.convertList(list);
			if (dtoList != null) {
				for (MerchantDto dto : dtoList) {
					cache.put(dto.getId(), dto);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static MerchantDto get(String id) {
		if (cache.containsKey(id)) {
			return cache.get(id);
		} else {
			return null;
		}

	}
}
